package com.dykj.fireFighting.web.base.system.controller;

import com.dykj.fireFighting.api.base.system.dto.DirectoryTypeDto;
import com.dykj.fireFighting.api.base.system.entity.SysDirectoryType;
import com.dykj.fireFighting.api.base.system.service.ISysDirectoryTypeService;
import com.dykj.fireFighting.api.base.system.vo.SysDirectoryTypeVo;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * 数据字典类型接口
 *
 * @Author hxb
 * @Date 2021-06-21 09:51:28
 */
@Slf4j
@Api(value = "数据字典类型接口", tags = {"数据字典类型接口"})
@RestController
@RequestMapping("system/sysDirectoryType")
public class SysDirectoryTypeController {


    @DubboReference
    private ISysDirectoryTypeService sysDirectoryTypeService;


    /**
     * 数据字典类型列表
     *
     * @return
     */
    @ApiOperation(value = "获取数据字典类型列表", notes = "获取数据字典类型列表")
    @GetMapping("/list")
    @RequiresPermissions("system/sysDirectoryType/list")
    public CommonResult<PageVo<SysDirectoryTypeVo>> list(DirectoryTypeDto directoryTypeDto) {
        PageVo<SysDirectoryTypeVo> page = sysDirectoryTypeService.queryDirectoryTypeList(directoryTypeDto);
        return CommonResult.success(page);
    }

    /**
     * 获取数据字典类型信息
     */
    @AutoLog(value = "数据字典类型-获取数据字典类型信息")
    @ApiOperation(value = "获取数据字典类型信息", notes = "获取数据字典类型信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "Integer", required = true)
    @GetMapping("/info/{id}")
    @RequiresPermissions("system/sysDirectoryType/info")
    public CommonResult<SysDirectoryType> info(@PathVariable("id") Integer id) {
        SysDirectoryType sysDirectoryType = sysDirectoryTypeService.getById(id);
        return CommonResult.success(sysDirectoryType);
    }

    /**
     * 保存数据字典类型信息
     */
    @AutoLog(value = "数据字典类型-保存数据字典类型信息")
    @ApiOperation(value = "保存数据字典类型信息", notes = "保存数据字典类型信息")
    @PostMapping("/save")
    @RequiresPermissions("system/sysDirectoryType/save")
    public CommonResult save(@Valid @RequestBody SysDirectoryType sysDirectoryType) {
        try {
            String userId = Long.toString(ShiroUtil.getUser().getId());
            if (StringUtils.isBlank(sysDirectoryType.getParentKeyCode())) {
                sysDirectoryType.setParentKeyCode("-1");
            }
            sysDirectoryType.setCreateUser(userId);
            sysDirectoryType.setCreateTime(new Date());
            sysDirectoryTypeService.save(sysDirectoryType);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改数据字典类型信息
     */
    @AutoLog(value = "数据字典类型-修改数据字典类型信息")
    @ApiOperation(value = "修改数据字典类型信息", notes = "修改数据字典类型信息")
    @PostMapping("/update")
    @RequiresPermissions("system/sysDirectoryType/update")
    public CommonResult update(@Valid @RequestBody SysDirectoryType sysDirectoryType) {
        try {
            String userId = Long.toString(ShiroUtil.getUser().getId());
            sysDirectoryType.setUpdateUser(userId);
            sysDirectoryType.setUpdateTime(new Date());
            sysDirectoryTypeService.updateById(sysDirectoryType);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 删除数据字典类型信息
     */
    @AutoLog(value = "数据字典类型-删除数据字典类型信息")
    @ApiOperation(value = "删除数据字典类型信息", notes = "删除数据字典类型信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "Integer", required = true, allowMultiple = true)
    @PostMapping("/delete")
    @RequiresPermissions("system/sysDirectoryType/delete")
    public CommonResult delete(@RequestBody Integer[] ids) {
        try {
            sysDirectoryTypeService.removeByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 获取所有数据字典类型
     *
     * @return
     */
    @ApiOperation(value = "获取数据字典类型列表", notes = "获取数据字典类型列表")
    @GetMapping("/allList")
    public CommonResult<List<SysDirectoryType>> allList() {
        List<SysDirectoryType> sysDirectoryTypeList = sysDirectoryTypeService.queryDirectoryTypeAllList();
        return CommonResult.success(sysDirectoryTypeList);
    }
}
